package cn.bone.auth.security.base.advice;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    //没有权限
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public JSONObject exception403(AccessDeniedException e){

        System.out.println("exception403---- " + e.getMessage());
        e.printStackTrace();

        JSONObject object = new JSONObject();
        object.put("message",e.getMessage());
        object.put("code","101");
        object.put("data","没有权限");

        return object;
    }



    //服务器错误
    @ExceptionHandler(Exception.class)
    public JSONObject exception500(Exception e){

        System.out.println("exception500---- " + e.getMessage());
        e.printStackTrace();

        JSONObject object = new JSONObject();
        object.put("message",e.getMessage());
        object.put("code","500");
        object.put("data","服务器错误");

        return object;
    }




}
